<?php
/**
 * @version 	$Id: fFilter.class.php 74 2010-03-17 06:48:21Z mylifeisskidrow@gmail.com $
 * @author 		$Author: mylifeisskidrow@gmail.com $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */

defined('IN_FLYWHEEL') or die('Restricted Access');

/**
 * Filter
 * 
 * @package 	system
 * @subpackage 	filter
 *
 */
class fFilter {
	const TYPE_STRING = 1;
	const TYPE_INT = 2;
	const TYPE_FLOAT = 3;
	const TYPE_DOUBLE = 4;	
	const TYPE_BOOLEAN = 5;
	const TYPE_ALNUM = 6;
	const TYPE_BASE64 = 7;
	const TYPE_CMD = 8;	
	const TYPE_ARRAY = 9;
	
	public static function clean($value, $type = self::TYPE_STRING) {
		if ($value === null) return null;
		
		switch ($type) {
			case self::TYPE_INT:
				preg_match('/-?[0-9]+/', (string) $value, $matches);
				$result = @ (int) $matches[0];
				break;
			case self::TYPE_FLOAT:
			case self::TYPE_DOUBLE:
				preg_match('/-?[0-9]+(\.[0-9]+)?/', (string) $value, $matches);
				$result = @ (float) $matches[0];
				break;
			case self::TYPE_BOOLEAN:
				$result = (boolean) $value;
				break;
			case self::TYPE_ALNUM:
				$result = (string) preg_replace( '/[^A-Z0-9]/i', '', $value);
				break;
			case self::TYPE_CMD:
				$result = (string) preg_replace( '/[^A-Z0-9_\.-]/i', '', $value);
				$result = ltrim($result, '.');
				break;
			case self::TYPE_ARRAY:
				$result = (array) $value;
				break;
			case self::TYPE_BASE64:				
				$result = (string) preg_replace( '/[^A-Z0-9\/+=]/i', '', $value);
				break;
			case self::TYPE_STRING:
			default:
				$result = trim((string) $value);
				break;
		}
		
		return $result;
	}
}